using System.Reflection;
namespace Net35.Xrm.Sdk.Client
{
	public sealed class ProxyTypesBehavior : System.ServiceModel.Description.IEndpointBehavior
	{
		private Assembly _proxyTypesAssembly;
		private readonly object _lockObject = new object();
		public ProxyTypesBehavior()
		{
		}
		public ProxyTypesBehavior(Assembly proxyTypesAssembly)
		{
			this._proxyTypesAssembly = proxyTypesAssembly;
		}
		void System.ServiceModel.Description.IEndpointBehavior.AddBindingParameters(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
		{
		}
		void System.ServiceModel.Description.IEndpointBehavior.ApplyClientBehavior(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Dispatcher.ClientRuntime behavior)
		{
			foreach (System.ServiceModel.Description.OperationDescription current in serviceEndpoint.Contract.Operations)
			{
				this.UpdateFormatterBehavior(current);
			}
		}
		void System.ServiceModel.Description.IEndpointBehavior.ApplyDispatchBehavior(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
		{
		}
		void System.ServiceModel.Description.IEndpointBehavior.Validate(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint)
		{
		}
		private void UpdateFormatterBehavior(System.ServiceModel.Description.OperationDescription operationDescription)
		{
			lock (this._lockObject)
			{
				System.ServiceModel.Description.DataContractSerializerOperationBehavior dataContractSerializerOperationBehavior = operationDescription.Behaviors.Find<System.ServiceModel.Description.DataContractSerializerOperationBehavior>();
				if (dataContractSerializerOperationBehavior != null)
				{
					dataContractSerializerOperationBehavior.DataContractSurrogate = new ProxySerializationSurrogate(this._proxyTypesAssembly);
				}
				else
				{
					dataContractSerializerOperationBehavior = new System.ServiceModel.Description.DataContractSerializerOperationBehavior(operationDescription);
					operationDescription.Behaviors.Add(dataContractSerializerOperationBehavior);
				}
			}
		}
	}
}
